package com.eqdd.yiqidian.ui.mine.shenqing;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Parcelable;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;

import com.eqdd.annotation.datasave.ToSave;
import com.eqdd.yiqidian.ApplayLizhiActivityCustom;
import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.rebuild.ISelectBean;
import com.eqdd.yiqidian.bean.rebuild.RuleListResultBean;
import com.eqdd.yiqidian.bean.rebuild.RuleResultBean;
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditApplyTitleBean;
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditEnterBean;
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditHintBean;
import com.eqdd.yiqidian.http.DialogCallBack;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpResult;
import com.eqdd.yiqidian.ui.base.SlimBaseMultiEditActivity;
import com.eqdd.yiqidian.ui.select.MultiSelectBuilder;
import com.eqdd.yiqidian.utils.ListUtil;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import java.util.ArrayList;
import java.util.List;

public class ApplyChangeShiftActivity extends SlimBaseMultiEditActivity {
    private ApplayLizhiActivityCustom dataBinding;
    @ToSave
    String ruleId;
    private String msg;
    private List<RuleListResultBean> rules;

    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_applay_lizhi);
        initTopTitleBar(View.VISIBLE, "调班申请");
        initTopRightText("提交", v -> {
            if (checkParam()) {
                OkGo.<HttpResult>post(HttpConfig.BASE_URL + HttpConfig.CREATE_CHANGE_SHIFT)
                        .params("companyId", user.getCompanyId())
                        .params("changeShiftId", ruleId)
                        .params("userGuid", user.getGuid())
                        .params("changeShiftReason", setParam(3))
                        .execute(new DialogCallBack<HttpResult>(this) {
                            @Override
                            public void onSuccess(Response<HttpResult> response) {
                                HttpResult httpResult = response.body();
                                ToastUtil.showShort(httpResult.getMsg());
                                if (httpResult.getStatus() == 200) {
                                    isSave=false;
                                    clearData();
                                    setResult(Config.SUCCESS);
                                    finish();
                                }
                            }
                        });
            } else {
                ToastUtil.showShort(msg);
            }
        });
    }

    private boolean checkParam() {
        if (TextUtils.isEmpty(setParam(2))) {
            msg = "请选择要调的班别";

            return false;
        }
        if (TextUtils.isEmpty(setParam(3))) {
            msg = "请输入调班原因";
            return false;
        }
        return true;
    }


    @Override
    protected RecyclerView initRecyclerView() {
        return dataBinding.recyclerView;
    }

    @Override
    public void itemClick(int p) {
        switch (p) {
            case 2:
                OkGo.<HttpResult<List<RuleListResultBean>>>post(HttpConfig.BASE_URL + HttpConfig.RULE_LIST)
                        .params("companyId", user.getCompanyId())
                        .execute(new DialogCallBack<HttpResult<List<RuleListResultBean>>>(ApplyChangeShiftActivity.this) {
                            @Override
                            public void onSuccess(Response<HttpResult<List<RuleListResultBean>>> response) {
                                HttpResult<List<RuleListResultBean>> httpResult = response.body();
                                ToastUtil.showShort(httpResult.getMsg());
                                if (httpResult.getStatus() == 200) {
                                    new MultiSelectBuilder(R.layout.list_item_48)
                                            .setAllDatas(httpResult.getItems())
                                            .setSelectDatas(rules)
                                            .setCompareField("Id")
                                            .build(ApplyChangeShiftActivity.this, Config.MULTI_SELECT, (data, inject) -> {
                                                RuleListResultBean bean = (RuleListResultBean) data;
                                                inject.text(R.id.tv_1, bean.getRuleName())
                                                        .text(R.id.tv_2, bean.getRuleDescribe())
                                                        .text(R.id.tv_3, bean.getWeeks())
                                                        .text(R.id.tv_4, bean.getHolidays())
                                                        .text(R.id.tv_5, "节假日为上班的节假日（没有是节假日不用上班）");
                                            });
                                }

                            }
                        });
                break;
        }
    }

    @Override
    protected void setEditBeans() {
        datas.add(new SlimEditApplyTitleBean());
        datas.add(new SlimEditHintBean("所在班别", "", 1));
        datas.add(new SlimEditEnterBean("要调班别", "", 2, true, "请选择"));
        datas.add(new SlimEditEnterBean("调班原因", "", 3, "请输入"));
        datas.add(new SlimEditHintBean("审批人", "", 4));
    }

    @Override
    protected void loadNetData() {
        OkGo.<HttpResult<RuleResultBean>>post(HttpConfig.BASE_URL + HttpConfig.MY_RULE)
                .params("userGuid", user.getGuid())
                .params("companyId", user.getCompanyId())
                .execute(new DialogCallBack<HttpResult<RuleResultBean>>(ApplyChangeShiftActivity.this) {
                    @Override
                    public void onSuccess(Response<HttpResult<RuleResultBean>> response) {
                        HttpResult<RuleResultBean> httpResult = response.body();
                        ToastUtil.showShort(httpResult.getMsg());
                        if (httpResult.getStatus() == 200) {
                            slimAdapterEx.getDataItem(1).setContent(httpResult.getItems().getRuleName());
                            slimAdapterEx.notifyItemChanged(1);
                        }
                    }
                });
        OkGo.<HttpResult<String>>post(HttpConfig.BASE_URL + HttpConfig.GET_LEADER)
                .params("userGuid", user.getGuid())
                .params("companyId", user.getCompanyId())
                .execute(new DialogCallBack<HttpResult<String>>(ApplyChangeShiftActivity.this) {
                    @Override
                    public void onSuccess(Response<HttpResult<String>> response) {
                        HttpResult<String> httpResult = response.body();
                        ToastUtil.showShort(httpResult.getMsg());
                        if (httpResult.getStatus() == 200) {
                            slimAdapterEx.getData().get(4).setContent(httpResult.getItems());
                            slimAdapterEx.notifyItemChanged(4);
                        }
                    }
                });
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {

        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == Config.SUCCESS && requestCode == Config.MULTI_SELECT) {
            ArrayList<RuleListResultBean> list = data.getParcelableArrayListExtra("list");
            if (ListUtil.isEmpty(list)) {
                return;
            }
            rules = list;
            ruleId = rules.get(0).getId() + "";
            slimAdapterEx.getDataItem(2).setContent(rules.get(0).getRuleName());
            slimAdapterEx.notifyItemChanged(2);
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}
